﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class Projects : PageBaseClass
{
    MyDMSEntity myDmsEntity =new MyDMSEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        var projects = from p in myDmsEntity.Project
                       select new
                                  {
                                      p.Project_Title,
                                      p.Project_Desc,
                                      DepartmentName =
                           myDmsEntity.Department.FirstOrDefault(d => d.Department_Code == p.Department_Code),
                                      AssignedName = myDmsEntity.User.FirstOrDefault(u => u.User_Code == p.Assigned_To).FirstName + myDmsEntity.User.FirstOrDefault(u => u.User_Code == p.Assigned_To).LastName,
                                      p.Updation_Date,
                                      p.Creation_Date
                                  };
        this.gvProjects.DataSource = projects;
        this.gvProjects.DataBind();

        if (!IsPostBack)
        {
            List<Department> deptList = myDmsEntity.Department.ToList();

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = "Department_Name";
            ddlDept.DataValueField = "Department_Code";
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }
        }
    }

    private void BindTheUsers(int deptCode)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            this.ddlUsers.DataSource =
                from u in myDmsEntity.User.Where(r => r.Department_Code == deptCode)
                select new
                           {
                               u.User_Code,
                               UserName = u.FirstName + u.LastName
                           };

            this.ddlUsers.DataTextField = "UserName";// +", " + User.UserInfoFields.LastName.ToString();
            this.ddlUsers.DataValueField = "User_Code";
            this.ddlUsers.DataBind();
        }
        else
        {
            this.ddlUsers.DataSource =
                from u in myDmsEntity.User
                select new
                {
                    u.User_Code,
                    UserName = u.FirstName + u.LastName
                };

            this.ddlUsers.DataTextField = "UserName";// +", " + User.UserInfoFields.LastName.ToString();
            this.ddlUsers.DataValueField = "User_Code";
            this.ddlUsers.DataBind();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Project Proj = new Project();
        Proj.Project_Title = txtProjectTitle.Text;
        Proj.Project_Desc = txtDescription.Text;
        Proj.Creation_Date = DateTime.Parse(txtStartDate.Text);
        Proj.Assigned_To = int.Parse(ddlUsers.SelectedValue);

        Proj.Creation_By = Author.user_Code;
        Proj.Office_Code = Author.office_Code;
        Proj.Department_Code = int.Parse(ddlDept.SelectedValue);

        myDmsEntity.Project.AddObject(Proj);
        
        if (myDmsEntity.SaveChanges()>0)
        {
            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }
}
